package eu.jba.denshijisho;

import eu.jba.denshijisho.dao.NoteDao;
import eu.jba.denshijisho.entity.Note;
import eu.jba.denshijisho.exceptionhandler.ExceptionHandler;
import eu.jba.denshijisho.util.Constants;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;

public class EditNoteActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Thread.setDefaultUncaughtExceptionHandler( new ExceptionHandler( this ) );
		setContentView(R.layout.activity_edit_note);
		setFinishOnTouchOutside(false);
		final Bundle bundle = getIntent().getExtras();
		final Integer noteId = (Integer)bundle.getInt(Constants.NOTE_ID);
		final NoteDao noteDao = new NoteDao( this );
		noteDao.open();
		final Note note = noteDao.get( noteId );
		noteDao.close();
		 ((EditText) findViewById(R.id.textNote_update)).setText(
			 note.getNote()			 
		 );
	}
	
	public void closeDialog(View view) {
		setResult(RESULT_CANCELED);
		finish();
	}
	
	public void updateNote(View view){
		final String noteText = ((EditText) findViewById(R.id.textNote_update))
				.getText().toString();
		final Bundle bundle = getIntent().getExtras();
		final Integer noteId = (Integer)bundle.getInt(Constants.NOTE_ID);
		final NoteDao noteDao = new NoteDao( this );
		noteDao.open();
		final Note note = noteDao.get( noteId );
		note.setNote( noteText );
		noteDao.update(note);
		noteDao.close();
		setResult(RESULT_OK);
		finish();

	}
}
